from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse
import requests
import time
from bs4 import BeautifulSoup
import re
import json
import urllib

#返回结果格式
def resultData():
    data = {
        "websiteType" : 0,
        "titleName":"",
        "titleHref": "",
        "source":"",
        "label":"",
        "volume":"",
        "summary":"",
        "detailId":'',
        "publishTime" :"",
        "category" : "",
        "quote" :"",
        "abstract": "",
        "doi": "",
        "downLoad": "",
        "keyword": [],
        "author": [],

    }
    return data

def formData(response):

    px = re.compile("window.APS =([\s\S]*?)window.onload")
    data = px.findall(str(response.text))[0].strip()

    lis = (json.loads(data))["searchResults"]


    allData = []


    for li in lis:
        tempData = resultData()

        try:
            tempData["titleName"] = li["title"]
        except:
            pass
        try:
            tempData["titleHref"] = li["link"]
        except:
            pass

        try:
            tempData["doi"] = str(li["doi"])
        except:
            pass

        try:
            tempData["publishTime"] = li["date"]
        except:
            pass

        try:
            tempData["source"] = li["journal"]
        except:
            pass

        allData.append(tempData)

    return allData

def getData(keyword,authenticity_token):
    headers = {
        'authority': 'journals.aps.org',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36',
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'referer': 'https://journals.aps.org/',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
        # 'cookie': 'rack.session=BAh7CEkiCWNzcmYGOgZFRkkiMXNtTEg2VHkzd0pUWnVONEFocmNodW1GZEN0%0AN1ZOSkoraFNRYkRvWEpqZ2M9BjsARkkiDXRyYWNraW5nBjsARnsHSSIUSFRU%0AUF9VU0VSX0FHRU5UBjsAVEkiLTgxOGM4ZDAxYWVhNTI1NmI0MjRmODJiMDM3%0ANTQ4ODk1MjU3OTg2OWQGOwBGSSIZSFRUUF9BQ0NFUFRfTEFOR1VBR0UGOwBU%0ASSItMzVhOTYzOGNlODU4YzhjZDBhM2IwOWMyZjBmNDk2YzBhYzA2NWI4OAY7%0AAEZJIg9zZXNzaW9uX2lkBjsAVEkiRWVhYjEyNTk4NTExZTFmM2NkMjA2NmM5%0AMDBkMzZjYmY4ZDkyOGE5ZThjNDY1ZDMxMGJkNjdhM2M3ZWVhZDRiMTcGOwBG%0A--a402f9088bee333c9416ffb0dda129d7f6dac78b; ajs_user_id=null; ajs_group_id=null; ajs_anonymous_id=%22ccda2b2b-c1fc-4fcc-8153-2af87d268ae9%22; _ga=GA1.2.154676262.1549940874; _gid=GA1.2.658611159.1549940874; mp_8b2ab50fd2e6ba68e73033ac10f46969_mixpanel=%7B%22distinct_id%22%3A%20%22168dfacd90261f-0110ca050af543-13396850-13c680-168dfacd903b16%22%2C%22%24device_id%22%3A%20%22168dfacd90261f-0110ca050af543-13396850-13c680-168dfacd903b16%22%2C%22mp_lib%22%3A%20%22Segment%3A%20web%22%2C%22%24initial_referrer%22%3A%20%22https%3A%2F%2Fjournals.aps.org%2F%22%2C%22%24initial_referring_domain%22%3A%20%22journals.aps.org%22%7D',
    }

    params = (
        ('authenticity_token', authenticity_token),
        ('sort', 'relevance'),
        ('q', keyword),
    )

    response = requests.get('https://journals.aps.org/search', headers=headers, params=params)

    allData = formData(response)


    return allData

def getToken():


    headers = {
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36',
    }

    response = requests.get('https://journals.aps.org/', headers=headers)

    soup = BeautifulSoup(response.text, "html5lib")

    authenticity_token = soup.select('input[name="authenticity_token"]')[0]["value"]

    return authenticity_token


def index(request):

    data = json.loads(request.POST.get('data', '1'))

    authenticity_token = getToken()

    allData = getData(data["values"][0],authenticity_token)

    return HttpResponse(json.dumps({
        "collection": allData,
        "allNum": 0
    }), content_type="application/json")